package cn.iocoder.yudao.module.reclaim.controller.admin.kefu;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.reclaim.controller.admin.kefu.vo.AdminKefuPageReqVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.kefu.vo.AdminKefuRespVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.kefu.vo.AdminKefuSaveReqVO;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.kefu.AdminKefuDO;
import cn.iocoder.yudao.module.reclaim.service.kefu.AdminKefuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 客服")
@RestController
@RequestMapping("/reclaim/admin-kefu")
@Validated
public class AdminKefuController {

    @Resource
    private AdminKefuService adminKefuService;

    @PostMapping("/create")
    @Operation(summary = "创建客服")
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:create')")
    public CommonResult<Long> createAdminKefu(@Valid @RequestBody AdminKefuSaveReqVO createReqVO) {
        return success(adminKefuService.createAdminKefu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服")
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:update')")
    public CommonResult<Boolean> updateAdminKefu(@Valid @RequestBody AdminKefuSaveReqVO updateReqVO) {
        adminKefuService.updateAdminKefu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:delete')")
    public CommonResult<Boolean> deleteAdminKefu(@RequestParam("id") Long id) {
        adminKefuService.deleteAdminKefu(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:query')")
    public CommonResult<AdminKefuRespVO> getAdminKefu(@RequestParam("id") Long id) {
        AdminKefuDO adminKefu = adminKefuService.getAdminKefu(id);
        return success(BeanUtils.toBean(adminKefu, AdminKefuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服分页")
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:query')")
    public CommonResult<PageResult<AdminKefuRespVO>> getAdminKefuPage(@Valid AdminKefuPageReqVO pageReqVO) {
        PageResult<AdminKefuDO> pageResult = adminKefuService.getAdminKefuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdminKefuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服 Excel")
    @PreAuthorize("@ss.hasPermission('reclaim:admin-kefu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAdminKefuExcel(@Valid AdminKefuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdminKefuDO> list = adminKefuService.getAdminKefuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服.xls", "数据", AdminKefuRespVO.class,
                        BeanUtils.toBean(list, AdminKefuRespVO.class));
    }

}